Write the new version of the persisted config to a tempfile and then rename it,
authorEwan Mellor <ewan@xensource.com>
Fri, 15 Dec 2006 17:19:00 +0000 (17:19 +0000)
committerEwan Mellor <ewan@xensource.com>
Fri, 15 Dec 2006 17:19:00 +0000 (17:19 +0000)
to avoid corrupting the file on failure.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
tools/python/xen/xend/XendDomain.py

index 464952519844fe189b0a5320fa4285a5b0a93b85..b0a8de6a0a5a650b2b6d9a94ac0ac4626ce61dda 100644 (file)
@@ -26,6 +26,7 @@ import os
 import stat
 import shutil
 import socket
+import tempfile
 import threading
 
 import xen.lowlevel.xc
@@ -280,16 +281,20 @@ class XendDomain:
             make_or_raise(domain_config_dir)
 
             try:
-                sxp_cache_file = open(self._managed_config_path(dom_uuid),'w')
-                prettyprint(dominfo.sxpr(), sxp_cache_file, width = 78)
-                sxp_cache_file.close()
+                fd, fn = tempfile.mkstemp()
+                f = os.fdopen(fd, 'w+b')
+                try:
+                    prettyprint(dominfo.sxpr(), f, width = 78)
+                finally:
+                    f.close()
+                try:
+                    os.rename(fn, self._managed_config_path(dom_uuid))
+                except:
+                    log.exception("Renaming %s" % fn)
+                    os.remove(fn)
             except:
                 log.exception("Error occurred saving configuration file " +
                               "to %s" % domain_config_dir)
-                try:
-                    self._managed_domain_remove(dom_uuid)
-                except:
-                    pass
                 raise XendError("Failed to save configuration file to: %s" %
                                 domain_config_dir)
         else: